﻿
using System.IO.IsolatedStorage;
using eLearning.Model;

namespace eLearning.Util
{
    public class SavedUser
    {
        public User User { get; set; }

        private static IsolatedStorageSettings appSettings;

        private const string idKey = "idKey";
        private const string userIdKey = "userIdKey";
        private const string passwordKey = "passwordKey";
        private const string usernameKey = "usernameKey";
        private const string namaKey = "namaKey";
        private const string userTypeKey = "userTypeKey";
        private const string savedKey = "savedKey";

        private static readonly SavedUser savedUser = new SavedUser();

        public static SavedUser Instance
        {
            get
            {
                appSettings = IsolatedStorageSettings.ApplicationSettings;
                return savedUser;
            }
        }

        public void SetUser(User user)
        {
            this.User = user;
            if (user != null)
            {
                appSettings.Remove(idKey);
                appSettings.Remove(userIdKey);
                appSettings.Remove(usernameKey);
                appSettings.Remove(userTypeKey);
                appSettings.Remove(namaKey);

                appSettings.Add(idKey, user.Id);
                appSettings.Add(userIdKey, user.IdUser);
                appSettings.Add(usernameKey, user.Username);
                appSettings.Add(userTypeKey, user.Type);
                appSettings.Add(namaKey, user.Nama);

                appSettings.Remove(savedKey);
                appSettings.Remove(passwordKey);
                if (user.Saved == true)
                {
                    appSettings.Add(savedKey, true);
                    appSettings.Add(passwordKey, user.Password);
                }
                else
                {
                    appSettings.Add(savedKey, false);
                    appSettings.Add(passwordKey, user.Password);
                }
            }
        }

        public void LogOut()
        {
            User.Saved = false;
            User.Password = null;
            SetUser(User);
        }

        public void DestroyUser()
        {
            User = null;
        }
        
        public User LoadUser()
        {
            User userCache = new User();
            if (appSettings.Contains(idKey) && appSettings.Contains(userIdKey) && appSettings.Contains(usernameKey) &&
                appSettings.Contains(namaKey) && appSettings.Contains(userTypeKey)
                && appSettings.Contains(passwordKey) && appSettings.Contains(savedKey))
            {
                userCache.Id = (string)appSettings[idKey];
                userCache.IdUser = (string)appSettings[userIdKey];
                userCache.Nama = (string)appSettings[namaKey];
                userCache.Username = (string)appSettings[usernameKey];
                userCache.Type = (User.UserType)appSettings[userTypeKey];
                userCache.Password = (string)appSettings[passwordKey];
                userCache.Saved = (bool)appSettings[savedKey];
            }
            else
            {
                userCache = null;
            }

            User = userCache;

            return User;
        }
    }
}
